package com.yzt.act.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yzt.base.kit.Base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;

@RestController()
@RequestMapping("act/activiti7")
@Api("Activiti7")
public class Activiti7Controller {

	@Autowired
	private RepositoryService repositoryService;

	@GetMapping("getProcess")
	public Base<Object> getProcess() {
		List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().latestVersion().list();
		List<Map<String, Object>> datas = new ArrayList<>();
		for (ProcessDefinition process : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", process.getId());
			map.put("key", process.getKey());
			map.put("name", process.getName());
			map.put("version", process.getVersion());
			map.put("deploymentId", process.getDeploymentId());

			datas.add(map);
		}
		return Base.okData(datas);
	}

	@GetMapping("getProcessTask")
	@ApiImplicitParam(name = "definitionId", value = "流程定义Id", paramType = "query")
	public Base<Object> getProcessTask(String definitionId) {
		BpmnModel bpmnModel = repositoryService.getBpmnModel(definitionId);
		Process process = bpmnModel.getProcesses().get(0);
		Collection<FlowElement> flowElements = process.getFlowElements();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (FlowElement flowElement : flowElements) {
			if (flowElement instanceof UserTask) {
				UserTask task = (UserTask) flowElement;
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("did", definitionId);
				map.put("taskId", task.getId());
				map.put("taskName", task.getName());
				list.add(map);
			}
		}
		return Base.okData(list);
	}

}
